package com.sky.Service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sky.Dao.CategoryDao;
import com.sky.Dao.DishDao;
import com.sky.Dao.DishFlavorDao;
import com.sky.Service.DishService;
import com.sky.constant.StatusConstant;
import com.sky.context.BaseContext;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Category;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.result.PageResult;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class DishServiceImpl
        extends ServiceImpl<DishDao, Dish>
        implements DishService {

    @Autowired
    private DishDao dishDao;
    @Autowired
    private CategoryDao categoryDao;
    @Autowired
    private DishFlavorDao dishFlavorDao;

    //TODO 分页逻辑还需要修改
    @Override
    public PageResult getByPage(DishPageQueryDTO dishPageQueryDTO) {
        IPage<Dish> page = new Page<>(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        IPage<DishVO> dishVOPage = new Page<>();
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        //菜品名称模糊查询
        queryWrapper.like(dishPageQueryDTO.getName() != null, Dish::getName, dishPageQueryDTO.getName());
        //菜品分类条件查询
        queryWrapper.eq(dishPageQueryDTO.getCategoryId() != null, Dish::getCategoryId, dishPageQueryDTO.getCategoryId());
        //售卖状态条件查询
        queryWrapper.eq(dishPageQueryDTO.getStatus() != null, Dish::getStatus, dishPageQueryDTO.getStatus());
        queryWrapper.orderByAsc(Dish::getName).orderByDesc(Dish::getUpdateTime);
        //查询
        dishDao.selectPage(page, queryWrapper);
        //将分页对象转换为自定义分页对象
        BeanUtils.copyProperties(page, dishVOPage, "records");
        //单独来处理records属性，将菜品对象转换为VO对象
        List<Dish> records = page.getRecords();

        List<DishVO> dishVOList = records.stream().map(item -> {
            //新建一个DishVO对象
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(item, dishVO);
            Long categoryId = item.getCategoryId();
            //根据categoryId查询分类名称
            Category category = categoryDao.selectById(categoryId);
            //获取分类名称
            String categoryName = category.getName();
            dishVO.setCategoryName(categoryName);
            return dishVO;
        }).collect(Collectors.toList());

        PageResult pageResult = new PageResult();
        //设置总记录数
        pageResult.setTotal(page.getTotal());
        //设置每页显示记录数
        pageResult.setRecords(dishVOList);
        return pageResult;


    }

    @Override
    public void addDishInfoWithFlavor(DishDTO dishDTO) {
        Dish dish = new Dish();
        BeanUtils.copyProperties(dishDTO, dish, "flavors");
        System.out.println("菜品信息:" + dish);
        //将菜品信息添加到对应的数据库表中
        dishDao.insert(dish);
        //将菜品的口味信息添加到对应的数据库表中
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> {
            System.out.println(flavor.getName() + "   " + flavor.getValue());
            //设置口味对应的菜品id
            flavor.setDishId(dish.getId());
            //将口味信息添加到对应的数据库表中
            dishFlavorDao.insert(flavor);
        });
    }


    @Override
    public void updateDishStatus(Integer status, Long id) {
        //根据id查询菜品信息
        Dish dish = dishDao.selectById(id);
        //修改菜品状态
        dish.setStatus(status);
        //更新修改时间
        dish.setUpdateTime(LocalDateTime.now());
        //更新修改人
        dish.setUpdateUser(BaseContext.getCurrentEmpId());
        //更新菜品信息
        dishDao.updateById(dish);
    }

    @Override
    public DishDTO getDishById(Long id) {
        //查询菜品基本信息
        Dish dish = dishDao.selectById(id);
        //新建一个DishDTO对象
        DishDTO dishDTO = new DishDTO();
        //属性拷贝
        BeanUtils.copyProperties(dish, dishDTO);
        //新建一个条件查询对象
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        //添加条件查询条件
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        //查询菜品对应口味信息
        List<DishFlavor> dishFlavors = dishFlavorDao.selectList(queryWrapper);
        //将口味信息添加到DishDTO对象中
        dishDTO.setFlavors(dishFlavors);
        return dishDTO;


    }


    @Override
    public void modifyDishInfo(DishDTO dishDTO) {
        //更新菜品表基本信息
        Dish dish = dishDao.selectById(dishDTO.getId());
        //属性拷贝
        BeanUtils.copyProperties(dishDTO, dish, "flavors");
        //更新修改时间
        dish.setUpdateTime(LocalDateTime.now());
        //更新修改人
        dish.setUpdateUser(BaseContext.getCurrentEmpId());
        System.out.println("修改后的菜品基本信息:" + dish);
        dishDao.updateById(dish);
        //单独来处理口味信息
        //清理当前菜品对应的口味数据
        System.out.println("清理当前菜品对应的口味数据");
        LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(DishFlavor::getDishId, dish.getId());
        dishFlavorDao.delete(queryWrapper);
        //添加当前提交过来的口味数据
        List<DishFlavor> flavors = dishDTO.getFlavors();
        flavors.forEach(flavor -> {
            //设置当前口味信息对应的菜品id
            flavor.setDishId(dish.getId());
            //更新修改时间
            flavor.setUpdateTime(LocalDateTime.now());
            //更新修改人
            flavor.setUpdateUser(BaseContext.getCurrentEmpId());
            //添加口味信息
            dishFlavorDao.insert(flavor);
        });
    }

    @Override
    public void deleteDishByIds(Long[] ids) {
        //先删除菜品所对应的口味信息
        for (Long id : ids) {
            //删除菜品对应的口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, id);
            dishFlavorDao.delete(queryWrapper);
            //删除菜品基本信息
            dishDao.deleteById(id);
        }
    }

    @Override
    public List<Dish> selectDishesByCategoryId(Long categoryId) {
        //查询分类下的所有菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, categoryId);
        List<Dish> dishes = dishDao.selectList(queryWrapper);
        return dishes;
    }

    @Override
    public List<DishVO> getDishListById(Long categoryId) {
        //查询分类下的所有菜品
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Dish::getCategoryId, categoryId);
        queryWrapper.eq(Dish::getStatus, StatusConstant.ENABLE);
        queryWrapper.orderByDesc(Dish::getPrice).orderByAsc(Dish::getUpdateTime);
        List<Dish> dishes = dishDao.selectList(queryWrapper);
        //将菜品对象转换为VO对象
        List<DishVO> dishVOList = dishes.stream().map(item -> {
            //新建一个DishVO对象
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(item, dishVO);
            //根据categoryId查询分类名称
            Category category = categoryDao.selectById(categoryId);
            //获取分类名称
            String name = category.getName();
            //设置分类名称
            dishVO.setCategoryName(name);
            //查询菜品对应的口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(DishFlavor::getDishId, item.getId());
            List<DishFlavor> dishFlavors = dishFlavorDao.selectList(queryWrapper1);
            //设置口味信息
            dishVO.setFlavors(dishFlavors);
            return dishVO;
        }).collect(Collectors.toList());
        return dishVOList;

    }


}
